package com.oa.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.pojo.Emp;
import com.oa.service.admin.AdminService;
import com.oa.vo.EmpVo;
import com.oa.vo.FileUploadVo;
import com.oa.vo.PageResult;
import com.oa.vo.SysResult;
import com.qiniu.common.QiniuException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @Author: 一拳超人
 * @Date: 2021/10/7 19:51
 */
@Api("用户管理数据接口")
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminUserController {
    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "获取用户信息", notes = "用户信息分页查询")
    @ApiImplicitParam(name = "pageResult", value = "分页vo", required = true, dataType = "PageResult", paramType = "path")
    @GetMapping("/userList")
    public SysResult selectUserInfo(PageResult pageResult) {
        Page<EmpVo> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        page = adminService.selectUserInfo(page, pageResult);
        pageResult.setRows(page.getRecords()).setTotal(page.getTotal());
        return SysResult.success(pageResult);
    }

    @ApiOperation(value = "更新用户状态", notes = "将用户状态设为离职或在职")
    @ApiImplicitParam(name = "emp", value = "用户实体类", required = true, dataType = "Emp", paramType = "query")
    @PutMapping("/updateEmpStatus")
    public SysResult updateEmpStatus(@RequestBody Emp emp) {
        return SysResult.success(adminService.updateEmpStatus(emp));
    }

    @ApiOperation(value = "删除指定员工", notes = "根据员工id删除员工")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "Integer", paramType = "path")
    @DeleteMapping("/deleteEmp/{id}")
    public SysResult deleteEmp(@PathVariable Integer id) {
        Integer integer = adminService.deleteEmp(id);
        if (integer == 201) {
            return SysResult.fail("该员工是部门主管,暂不可删除");
        }
        return SysResult.success();
    }

    @ApiOperation(value = "删除指定员工", notes = "根据员工id删除员工")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/addEmpInfo/generateEmpId")
    public SysResult generateEmpId() {
        return SysResult.success(adminService.generateEmpId());
    }

    /**
     * 获取部门信息
     *
     * @param level 部门级别
     * @return 查询结果
     */
    @ApiOperation(value = "删除指定员工", notes = "根据员工id删除员工")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/addEmpInfo/getDepartmentInfo/{level}")
    public SysResult getDepartment(@PathVariable Integer level) {
        return SysResult.success(adminService.getDepartmentInfo(level));
    }

    /**
     * 根据部门信息获取职位信息
     *
     * @param depId 部门id
     * @return 职位信息
     */
    @ApiOperation(value = "删除指定员工", notes = "根据员工id删除员工")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/addEmpInfo/getPositionInfo/{depId}")
    public SysResult getPositionInfo(@PathVariable Integer depId) {
        return SysResult.success(adminService.getPositionInfo(depId));
    }

    /**
     * 获取学历列表
     *
     * @return 学历信息
     */
    @ApiOperation(value = "删除指定员工", notes = "根据员工id删除员工")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/addEmpInfo/getEduInfo")
    public SysResult getEduInfo() {
        return SysResult.success(adminService.getEduInfo());
    }

    @ApiOperation(value = "删除指定员工", notes = "根据员工id删除员工")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "Integer", paramType = "path")
    @PostMapping("/addEmpInfo/submit")
    public SysResult addNewEmp(@RequestBody Emp emp) {
        return SysResult.success(adminService.addNewEmp(emp));
    }

    @ApiOperation(value = "删除指定员工", notes = "根据员工id删除员工")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "Integer", paramType = "path")
    @PutMapping("/updateEmpInfo")
    public SysResult updateEmpInfo(@RequestBody Emp emp) {
        if (emp.getResignDate() != null && emp.getEmpStatus()) {
            emp.setEmpStatus(false);
        }
        if (emp.getResignDate() == null && !emp.getEmpStatus()) {
            emp.setEmpStatus(true);
        }
        return SysResult.success(adminService.updateEmpInfo(emp));
    }

    @ApiOperation(value = "删除指定员工", notes = "根据员工id删除员工")
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/getEmpInfo/{id}")
    public SysResult getEmpInfo(@PathVariable Integer id) {
        return SysResult.success(adminService.getEmpInfo(id));
    }

    @PostMapping("/uploadEmpPhoto")
    public SysResult uploadEmpPhoto(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        assert fileName != null;
        fileName = fileName.toLowerCase();
        String regex = "^.+\\.(jpg|png|gif)$";
        if (!fileName.matches(regex)) {
            return SysResult.fail();
        }
        BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
        int height = bufferedImage.getHeight();
        int width = bufferedImage.getWidth();
        if (height == 0 || width == 0) {
            return SysResult.fail();
        }
        FileUploadVo fileUploadVo = adminService.uploadEmpPhoto(file.getInputStream());
        if (fileUploadVo.getUrlPath().equals("201")) {
            return SysResult.fail();
        }
        return SysResult.success(fileUploadVo);
    }

    @DeleteMapping("/deleteEmpPhoto")
    public SysResult deleteEmpPhoto(String key) throws QiniuException {
        key = key.substring(key.lastIndexOf("/") + 1);
        Integer status = adminService.deleteEmpPhoto(key);
        if (status != 200) {
            return SysResult.fail(status);
        }
        return SysResult.success(status);
    }
}
